package com.bulletelc.apollopre.module.main.center.quotation.detail;

import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.bulletelc.apollopre.R;
import com.bulletelc.apollopre.constants.IntentCode;
import com.bulletelc.apollopre.entity.OrderDataBean;
import com.bulletelc.apollopre.entity.QuotationBean;
import com.bulletelc.apollopre.framework.MVPBaseActivity;
import com.bulletelc.apollopre.module.deal.pay.PayCenterActivity;
import com.bulletelc.apollopre.module.display.product.details.ProductDetailsActivity;
import com.bulletelc.apollopre.module.main.center.orders.detail.OrderDetailActivity;
import com.bulletelc.apollopre.utils.ToastUtil;
import com.bulletelc.apollopre.widget.FoolBar;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2018/5/16.
 */

public class QuotationDetailActivity extends MVPBaseActivity<QuotationDetailContract.View, QuotationDetailPresenter> implements QuotationDetailContract.View,
        FoolBar.ClickCallback, View.OnClickListener, AdapterView.OnItemClickListener {

    // data
    private String id; // 报价单ID
    private String saleOrderId = null; // 订单Id
    int feedback = 0; // 反馈次数
    private QuotationBean quotationBean;
    private List<QuotationBean.ActiveItemsBean> activeItemsBeanList;
    private QuotationDetailAdapter adapter;
    private static int CANCEL = 40; // 取消
    private static int EDIT = 20; // 反馈

    // views
    private FoolBar flBar;
    private ListView lvProduct;
    private TextView tvTotal, tvEdit;
    private Button btnCancel, btnSubmit;
    private RelativeLayout rlBtn;

    @Override
    public int getLayoutRes() {
        return R.layout.quotation_detail_activity;
    }

    @Override
    public void assignViews() {
        flBar = findView(R.id.flBar);
        lvProduct = findView(R.id.lvProduct);
        tvTotal = findView(R.id.tvTotal);
        btnCancel = findView(R.id.btnCancel);
        tvEdit = findView(R.id.tvEdit);
        btnSubmit = findView(R.id.btnSubmit);
        rlBtn = findView(R.id.rlBtns);
    }

    @Override
    public void setListener() {
        flBar.setClickCallback(this);
        btnCancel.setOnClickListener(this);
        tvEdit.setOnClickListener(this);
        btnSubmit.setOnClickListener(this);
        lvProduct.setOnItemClickListener(this);
    }

    @Override
    public void initData(Bundle savedInstanceState) {
        flBar.setTitle("报价详情");
        Intent intent = getIntent();
        activeItemsBeanList = new ArrayList<>();
        if (intent != null) {
            id = intent.getStringExtra(IntentCode.EXTRA_QUOTATION_ORDER_ID);
        }
        adapter = new QuotationDetailAdapter(this, activeItemsBeanList);
        lvProduct.setAdapter(adapter);
        presenter.getQuotationDetail(id);
    }

    @Override
    public void onLeftClick() {
        finish();
    }

    @Override
    public void onRightClick() {

    }

    @Override
    protected QuotationDetailPresenter createPresenter() {
        return new QuotationDetailPresenter();
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btnCancel:
                showCancelDialog();
                break;
            case R.id.tvEdit:
                showEditDialog();
                break;
            case R.id.btnSubmit:
                submitOrder();
                break;
        }

    }

    @Override
    public void onSuccessQuotationDetail(QuotationBean quotationBean) {
        if (null != quotationBean) {
            this.quotationBean = quotationBean;
            this.activeItemsBeanList.clear();
            this.activeItemsBeanList.addAll(quotationBean.getActiveItems());
            if (null != quotationBean.getSaleOrder()) {
                this.saleOrderId = quotationBean.getSaleOrder().getId();
            }
            this.feedback = quotationBean.getFeedBackCount();
            adapter.notifyDataSetChanged();
            tvTotal.setText("¥" + quotationBean.getGrand_total());
            checkState(quotationBean.getState());
        }
    }

    @Override
    public void onErrorQuotationDetail(String error) {
        ToastUtil.showLongToast(this, R.string.network_msg);
    }

    @Override
    public void onFailQuotationDetail(String fail) {
        ToastUtil.showLongToast(this, fail);
    }

    @Override
    public void onSuccessCancel() {
        ToastUtil.showShortToast(this, "取消成功");
        rlBtn.setVisibility(View.GONE);
    }

    @Override
    public void onErrorCancel(String error) {
        ToastUtil.showLongToast(this, R.string.network_msg);
    }

    @Override
    public void onFailCancel(String fail) {
        ToastUtil.showLongToast(this, fail);
    }

    @Override
    public void onSuccessSubmit(OrderDataBean orderDataBean) {
        if (orderDataBean != null) {
            Intent intent = new Intent(this, PayCenterActivity.class);
            intent.putExtra(IntentCode.EXTRA_KEY_ORDER_DATA, orderDataBean);
            startActivity(intent);
            finish();
        } else {
            Toast.makeText(this, "数据发生错误", Toast.LENGTH_SHORT).show();
        }
    }

    @Override
    public void onErrorSubmit(String error) {
        ToastUtil.showLongToast(this, R.string.network_msg);
    }

    @Override
    public void onFailSubmit(String fail) {
        ToastUtil.showLongToast(this, fail);
    }

    @Override
    public void onSuccessEdit() {
        ToastUtil.showShortToast(this, "反馈成功");
        tvEdit.setText(R.string.quotation_edit_ag);
        feedback += 1;
    }

    @Override
    public void onErrorEdit(String error) {
        ToastUtil.showLongToast(this, R.string.network_msg);
    }

    @Override
    public void onFailEdit(String fail) {
        ToastUtil.showLongToast(this, fail);
    }

    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
        Intent intent = new Intent(this, ProductDetailsActivity.class);
        intent.putExtra(IntentCode.EXTRA_KEY_PRODUCT_ID, activeItemsBeanList.get(i).getProduct_id() + "");
        startActivity(intent);
    }

    private void submitOrder() {
        if (saleOrderId == null) {
            presenter.submit(Integer.parseInt(quotationBean.getId()));
        } else if (!"".equals(saleOrderId)) {
            Intent intent = new Intent(this, OrderDetailActivity.class);
            intent.putExtra(IntentCode.EXTRA_KEY_ORDER_ID, saleOrderId);
            startActivity(intent);
        }

    }

    /**
     * 显示用户确认取消订单的提示框
     */
    private void showCancelDialog() {
        AlertDialog dialog = new AlertDialog.Builder(this)
                .setMessage("您确定取消此订单吗？")
                .setNegativeButton("取消", null)
                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        presenter.cancel(CANCEL, Integer.parseInt(quotationBean.getId()));
                    }
                }).create();
        dialog.show();
    }

    /**
     * 检查订单状态
     * @param state
     */
    private void checkState(int state) {
        switch (state) {
            // 已发送给用户，可支付可取消可修改
            case 10:
                rlBtn.setVisibility(View.VISIBLE);
                break;
            // 客户反馈，可支付可取消可修改
            case 20:
                rlBtn.setVisibility(View.VISIBLE);
                break;
            // 客户确认，不可取消不可修改不可提交
            case 30:
                rlBtn.setVisibility(View.VISIBLE);
                btnCancel.setVisibility(View.GONE);
                tvEdit.setVisibility(View.GONE);
                btnSubmit.setText(R.string.quotation_detail);
                break;
            // 取消，不可取消不可修改不可提交
            case 40:

                break;
            default:

                break;
        }
    }

    /**
     * 显示用户确认修改订单的提示框
     */
    private void showEditDialog() {
        if (feedback >= 3) {
            ToastUtil.showShortToast(this, "反馈次数过多，您可以联系我们");
            return;
        }
        AlertDialog dialog = new AlertDialog.Builder(this)
                .setMessage("请确认是否向客服反馈修改报价单清单？")
                .setNegativeButton("取消", null)
                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        presenter.edit(EDIT, Integer.parseInt(quotationBean.getId()));
                    }
                }).create();
        dialog.show();
    }


}
